package com.netcracker.training.oracle.dao.user;

import com.netcracker.training.oracle.dao.common.AbstractDao;
import com.netcracker.training.oracle.dao.entity.User;
import com.netcracker.training.oracle.dao.manager.DBException;
import com.netcracker.training.oracle.dao.manager.DBManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author olhr0713
 */
public class SQLiteUserDao extends AbstractDao implements UserDao {

    protected static final String ADD_USER_SQL = "INSERT INTO USERS VALUES($next_id, ?, ?, ?)";
    protected static final String UPDATE_USER_SQL = "UPDATE USERS SET FIRST_NAME = ?, SECOND_NAME = ?, ADDRESS =? WHERE ID = ?";
    protected static final String DELETE_USER_BY_FIRST_NAME_SQL = "DELETE FROM USERS WHERE FIRST_NAME = ?";
    protected static final String DELETE_ALL_USERS_SQL = "DELETE FROM USERS";
    protected static final String LOAD_ALL_USERS_SQL = "SELECT * FROM USERS";

    public SQLiteUserDao(DBManager dBManager) {
        super(dBManager);
    }

    public void insertOrUpdateUser(User user) throws DBException {
        if (user.getId() == null || updateUser(user) == 0) {
            insertUser(user);
        }
    }
    
    private int updateUser(User user) throws DBException{
        PreparedStatement preparedStatement = null;

        try {
            preparedStatement = dBManager.prepareStatement(UPDATE_USER_SQL);
            preparedStatement.setString(1, user.getFirstName());
            preparedStatement.setString(2, user.getSecondName());
            preparedStatement.setString(3, user.getAddress());
            preparedStatement.setLong(4, user.getId());

            return preparedStatement.executeUpdate();
        } catch (SQLException ex) {
            throw new DBException(ex.getMessage(), ex);
        } finally {
            close(preparedStatement);
        }
    }

    private void insertUser(User user) throws DBException {
        PreparedStatement preparedStatement = null;

        try {
            preparedStatement = dBManager.prepareStatement(ADD_USER_SQL);
            preparedStatement.setString(2, user.getFirstName());
            preparedStatement.setString(3, user.getSecondName());
            preparedStatement.setString(4, user.getAddress());

            preparedStatement.executeUpdate();
        } catch (SQLException ex) {
            throw new DBException(ex.getMessage(), ex);
        } finally {
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException ex) {
                    throw new DBException(ex.getMessage(), ex);
                }
            }
        }
    }

    public void deleteUser(User user) throws DBException {
        PreparedStatement preparedStatement = null;

        try {
            preparedStatement = dBManager.prepareStatement(DELETE_USER_BY_FIRST_NAME_SQL);
            preparedStatement.setString(1, user.getFirstName());

            preparedStatement.executeUpdate();
        } catch (SQLException ex) {
            throw new DBException(ex.getMessage(), ex);
        } finally {
            close(preparedStatement);
        }
    }

    public void deleteAllUsers() throws DBException {
        dBManager.executeUpdate(DELETE_ALL_USERS_SQL);
    }

    public List<User> loadAllUsers() throws DBException {
        List<User> userList = new ArrayList<User>();
        ResultSet resultSet = null;

        try {
            resultSet = dBManager.execSQL(LOAD_ALL_USERS_SQL);

            User user;
            while (resultSet.next()) {
                user = new User();
                user.setId(resultSet.getLong(1));
                user.setFirstName(resultSet.getString(2));
                user.setSecondName(resultSet.getString(3));
                user.setAddress(resultSet.getString(4));
                userList.add(user);
            }
        } catch (SQLException ex) {
            throw new DBException(ex.getMessage(), ex);
        } finally {
            close(resultSet);
        }

        return userList;
    }
}
